# -*- coding: utf-8 -*-
"""
@file   : parser.py
@author : xh
@date   : 2021/8/3 10:33 上午
@desc   :
"""
import logging
import json
from utils.get_cleanQuestion import get_clean_question
import requests
from utils.config import Config

conf = Config(config_file='conf/config.json')
config = conf.config
syntacticParserUrl = config[conf.env]["syntacticParserUrl"]
logger = logging.getLogger(__name__)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')


def func(x):
    return x.replace(u"，", u",").replace(u"。", u".").replace("·", "")


def auto_parser(text):
    """
    公式解析
    """
    question = get_clean_question(text)
    raw = {"main": func(question["main"]),
           "options": [func(opt) for opt in question["options"]],
           "subquestions": [func(sub) for sub in question["subquestions"]]}
    logger.info('[SEP] raw: %s' % json.dumps(raw, ensure_ascii=False))
    try:
        t = requests.get(url=syntacticParserUrl, params=raw, timeout=5).json()
    except Exception as E:
        print(str(E))
        logger.info('[SEP] parser-failed: %s' % str(E))
        return -20, None
    if not t['exprs']:
        logger.info('[SEP] parser-failed: %s' % json.dumps(t, ensure_ascii=False))
        return -18, None
    else:
        logger.info('[SEP] parser-success: %s' % json.dumps(t, ensure_ascii=False))
        return 1, t


if __name__ == '__main__':
    pass
